# -*- coding: utf-8 -*-
"""
automatically create comments at some signals 
+ comment counter
"""

from django.db.models.signals import post_save, post_delete

from utils.templatetags.base_extras import userlink

from news.models import News
from projects.models import Project, Release, Screenshot

import models
from models import Comment

from time import mktime, time



def news_changed(sender, instance, created, **kwargs):
    if (not created and instance.active and 
        abs(time() - mktime(instance.created.timetuple())) > 60*10):   # don't show changes in first 10 min after publishing
        Comment(content_object=instance, 
                markup="raw", 
                content=("%s has made some changes" %
                         userlink(instance.last_editor))).save()
          

def new_release(sender, instance, created, **kwargs):
    if created:
        Comment(content_object=instance.project,
                markup="raw", 
                content=("%s has released <a href='%s'>%s</a>" %
                         (userlink(instance.last_editor),
                          instance.get_absolute_url(),
                          instance))).save()
                         
def delete_release(sender, instance, **kwargs):
    Comment(content_object=instance.project,
            markup="raw", 
            content="%s has been deleted" % instance).save()

def new_screenshot(sender, instance, created, **kwargs):
    if created:
        Comment(content_object=instance.project,
                markup="raw", 
                content=("%s has added screenshot <a href='%s'>%s</a>" %
                         (userlink(instance.uploaded_by), 
                         instance.get_absolute_url(),
                         instance))).save()
                         
def delete_screenshot(sender, instance, **kwargs):
    Comment(content_object=instance.project,
            markup="raw", 
            content=("screenshot %s has been deleted" % instance)).save()
            
def comment_counter_add(sender, instance, created, **kwargs):
    if (created and isinstance(instance.content_object, Project)
        and not instance.is_auto_comment()):
        instance.content_object.comment_count += 1
        instance.content_object.save()

def comment_counter_delete(sender, instance, **kwargs):
    if (isinstance(instance.content_object, Project)
        and not instance.is_auto_comment()):
        instance.content_object.comment_count -= 1
        instance.content_object.save()
 
 
# only add autocomments about new releases

#post_save.connect(news_changed, sender=News, dispatch_uid="news.News")
post_save.connect(new_release, sender=Release, dispatch_uid="projects.Release")
#post_delete.connect(delete_release, sender=Release,dispatch_uid="release.del")
#post_save.connect(new_screenshot, sender=Screenshot, dispatch_uid="scr")
#post_delete.connect(delete_screenshot, sender=Screenshot, dispatch_uid="delscr")

post_save.connect(comment_counter_add, sender=Comment, dispatch_uid="c+")
post_delete.connect(comment_counter_delete, sender=Comment, dispatch_uid="c-")
